﻿using Admin.主界面;
using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Admin.业务界面
{
    public partial class 学生数据 : Form
    {
        List<Student> all_stu;
        List<Student> stu_added = new List<Student>();
        int pagenumber = 1;
        int pageSize = 20;
        int totalCount = 0;
        int pageCount = 0;

        public 学生数据()
        {
            InitializeComponent();
        }

        private void 学生数据_Load(object sender, EventArgs e)//加载页面
        {
            pagenumber = 1;
            getPage(ref totalCount);
        }

        private void button5_Click(object sender, EventArgs e)//导入学生数据
        {
            if (openFileDialog1.ShowDialog() != DialogResult.OK)
                return;

            string Path = openFileDialog1.FileName;
            DataTable dt = Tools.Read_excel.read_excel(Path);
            if (dt == null) return;

            List<Student> students = new List<Student>();

            foreach (DataRow dr in dt.Rows)
            {
                Student stu = new Student();
                stu.stu_no = dr["学号"].ToString();
                stu.pwd = "123";
                stu.stu_name = dr["姓名"].ToString();
                stu.major_id = 1;
                stu.grade = 2021;
                stu.phone = dr["手机号码"].ToString();
                stu.group_id = 0;
                stu.is_gleader = 0;
                students.Add(stu);
            }
            BLL.B_Admin b_stu = new BLL.B_Admin();
            try
            {
                var res = b_stu.Add_S(students);
                pagenumber = 1;
                getPage(ref totalCount);
            }
            catch (Tools.Add_Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void button8_Click(object sender, EventArgs e)//保存修改
        {
            BLL.B_Admin b_Student = new BLL.B_Admin();
            try
            {
                if (b_Student.Save_List_S(all_stu))
                    MessageBox.Show("保存成功！");
            }
            catch (Tools.Modify_Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        void getPage(ref int totalCount)//换页功能
        {
            BLL.B_Admin b_Student = new BLL.B_Admin();
            all_stu = b_Student.GetPage_S(pagenumber, pageSize, ref totalCount);
            dataGridView1.DataSource = new BindingList<Student>(all_stu);
            pageCount = Convert.ToInt32(Math.Ceiling((double)totalCount / pageSize));
        }

        private void students_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            var stu = dataGridView1.CurrentRow.DataBoundItem as Student;
            stu.group_id = 0;
            stu_added.Add(stu);
        }

        private void button9_Click(object sender, EventArgs e)//删除学生数据
        {
            List<Student> stu_deleted = new List<Student>();
            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                stu_deleted.Add(row.DataBoundItem as Student);
            }
            BLL.B_Admin b_Student = new BLL.B_Admin();
            try
            {
                if (b_Student.Del_S(stu_deleted))
                    MessageBox.Show("删除成功！");

                getPage(ref totalCount);
            }
            catch (Tools.Del_Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }     

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)//首页
        {
            pagenumber = 1;
            getPage(ref totalCount);
        }

        private void button3_Click(object sender, EventArgs e)//上一页
        {
            if (pagenumber > 1)
            {
                pagenumber--;
                getPage(ref totalCount);
            }
        }

        private void button4_Click(object sender, EventArgs e)//下一页
        {
            if (pagenumber < pageCount)
            {
                pagenumber++;
                getPage(ref totalCount);
            }
        }

        private void button2_Click(object sender, EventArgs e)//尾页
        {
            pagenumber = pageCount;
            getPage(ref totalCount);
        }

        private void 学生数据_FormClosed(object sender, FormClosedEventArgs e)
        {
            //this.Hide();
            //管理员主界面 f1 = new 管理员主界面();
            //f1.ShowDialog();
        }
    }
}
